import Vue from "vue";

import App from "./App.vue";

import VueRouter from "vue-router";
Vue.use(VueRouter);

import UsersA from "./components/UsersA.vue";


const router = new VueRouter({
  routes: [
    {
      path: "/users/a",
      name: "UsersA",
      component: UsersA,
      meta: {
        //元数据
        name: "用户管理",
        hidden: false,
        logined: true,
        auth: ["admin"],
      },
    },
    {
      path: "/users/b",
      name: "UsersB",
      component: () => import(/* webpackChunkName: "group-foo" */ "./components/UsersB.vue"),
      meta: {
        //元数据
        name: "商城管理",
        hidden: false,
        logined: true,
        // auth: ["user"],
      },
    },
    {
      path: "/users/c",
      name: "UsersC",
      component: () => import(/* webpackChunkName: "abc" */ "./components/UsersC.vue"),
      meta: {
        //元数据
        name: "个人中心",
        hidden: false,
        logined: true,
        auth: ["user", "guest"],
      },
    },
    {
      path: "/users/d",
      name: "UsersD",
      // component: NotFound,
      component: () => import(/* webpackChunkName: "abc" */ "./components/NotFound.vue"),
      meta: {
        //元数据
        name: "订单中心",
        hidden: false,
        logined: true,
        auth: ["admin", "user", "guest"],
      },
    },
  ],
});

const flag = true; //登录
const auth = "admin";
router.beforeEach((to, from, next) => {
  if (to.meta?.logined === true) {
    // 判断是否登录
    if (flag) {
      // 判断权限
      if (to.meta?.auth === undefined || to.meta?.auth.includes(auth)) {
        next();
      } else {
        next(false);
      }
    } else {
      next(false);
    }
  } else {
    next();
  }
});

new Vue({
  render: (h) => h(App),
  router,
}).$mount("#app");
